package org.example.aaaatest.A01;

import java.util.Arrays;

/**
 * @description:
 * @author: wangbaogui
 * @create: 2025/3/24 11:42
 **/
/*
给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数。



示例 1:

输入: nums = [1,2,3,4,5,6,7], k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右轮转 1 步: [7,1,2,3,4,5,6]
向右轮转 2 步: [6,7,1,2,3,4,5]
向右轮转 3 步: [5,6,7,1,2,3,4]
示例 2:

输入：nums = [-1,-100,3,99], k = 2
输出：[3,99,-1,-100]
解释:
向右轮转 1 步: [99,-1,-100,3]
向右轮转 2 步: [3,99,-1,-100]

 */
public class A6 {
    public static void main(String[] args) {
        int [] nums={1,2,3,4,5,6,7};
        rotate1(nums,3);
        System.out.println("nums ========== " + Arrays.toString(nums));
    }

    public static void rotate(int[] nums, int k) {

        for (int i = 0; i < k; i++) {
            int num =nums[nums.length-1];
            for (int i1 = nums.length-1; i1 > 0; i1--) {
                nums[i1]=nums[i1-1];
            }
            nums[0] = num;

            System.out.println("nums = " + Arrays.toString(nums));

        }

    }

    public static void rotate1(int[] nums, int k) {
        int n = nums.length;
        int [] newNums = new int[n];
        for (int i = 0; i < nums.length; i++) {
            newNums[(i+k)% n] = nums[i];
        }

        System.arraycopy(newNums,0,nums,0,n);
    }
}
